using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class ZombiesInRoom : MonoBehaviour
{
	public static readonly char[] separator = new char[1]
	{
		','
	};

	public static int[] leftTotal = new int[0];

	public Transform container;

	public int room;

	public int roomMax = 15;

	public int zombiesMax = 50;

	[Space(6f)]
	public float wallRoom = 10f;

	private Vector3 startPosition;

	public float timeCrowd = 120f;

	private float currentTimerCrowd;

	public string sceneReplay = string.Empty;

	public string sceneGameOver = string.Empty;

	public ScreenTitleLevel titleLevel;

	public Text numZombies;

	public bool saveZombiesNum;

	private ManagerZombies[] managerZombies = new ManagerZombies[0];

	private Vector3 vec = default(Vector3);

	public static string currentNum
	{
		get
		{
			if (leftTotal.Length == 0)
			{
				return string.Empty;
			}
			string text = string.Empty;
			for (int i = 0; i < leftTotal.Length; i++)
			{
				text = text + leftTotal[i].ToString() + separator[0].ToString();
			}
			return text.TrimEnd(separator);
		}
		set
		{
			if (string.IsNullOrEmpty(value))
			{
				leftTotal = new int[0];
				return;
			}
			string[] array = value.Split(separator);
			leftTotal = new int[array.Length];
			for (int i = 0; i < array.Length && !string.IsNullOrEmpty(array[i]); i++)
			{
				leftTotal[i] = int.Parse(array[i]);
			}
		}
	}

	private void OnDrawGizmos()
	{
		if (base.enabled)
		{
			Gizmos.color = new Color(0f, 1f, 0.5f);
			vec = base.transform.position;
			Gizmos.DrawLine(vec, new Vector3(vec.x + wallRoom, vec.y + 3f, vec.y));
			Gizmos.DrawLine(new Vector3(vec.x + wallRoom, vec.y - 2f, vec.y), new Vector3(vec.x + wallRoom, vec.y + 5f, vec.y));
		}
	}

	private void Awake()
	{
		if (SettingsStartGame.gameMode != "none")
		{
			room = SettingsStartGame.currentCorridor;
		}
		managerZombies = base.gameObject.GetComponents<ManagerZombies>();
		float num = room;
		float num2 = roomMax;
		float num3 = Mathf.Floor(num / num2 * 1000f) / 1000f;
		float num4 = 0f;
		zombiesMax += Mathf.Max(0, room - roomMax);
		int num5 = Mathf.Clamp(room - roomMax, 0, 30);
		UnityEngine.Debug.Log("ZombiesInRoom.limitOnStage:" + num5.ToString());
		for (int i = 0; i < num5; i++)
		{
			managerZombies[i % managerZombies.Length].limitOnStage++;
		}
		for (int j = 0; j < managerZombies.Length; j++)
		{
			num4 += (float)managerZombies[j].total;
		}
		for (int k = 0; k < managerZombies.Length; k++)
		{
			managerZombies[k].periodAdd = Mathf.Lerp(managerZombies[k].periodAdd, 0f, num3);
			managerZombies[k].periodAdd = Mathf.Floor(managerZombies[k].periodAdd * 100f) / 100f;
			managerZombies[k].timeStart = Mathf.Lerp(managerZombies[k].timeStart, 2f, num3);
			managerZombies[k].timeStart = Mathf.Floor(managerZombies[k].timeStart * 100f) / 100f;
			managerZombies[k].scaleSpeedMove = Mathf.Lerp(managerZombies[k].scaleSpeedMove, managerZombies[k].scaleSpeedMoveMax, num3);
			managerZombies[k].scaleSpeedMove = Mathf.Round(managerZombies[k].scaleSpeedMove * 100f) / 100f;
			managerZombies[k].total += Mathf.RoundToInt((float)managerZombies[k].total / num4 * (float)zombiesMax * num3);
			if (saveZombiesNum && leftTotal.Length == managerZombies.Length)
			{
				managerZombies[k].total = Mathf.CeilToInt(Mathf.Lerp(leftTotal[k], managerZombies[k].total, num3 * 0.8f));
			}
		}
	}

	private void Start()
	{
		if (container != null)
		{
			base.transform.SetParent(container);
		}
		startPosition = base.transform.position;
		InvokeRepeating("CheckAlive", 10f, 5f);
		titleLevel.ShowStart(room + 1);
		Player.listenersDeath = OnDeathPlayer;
	}

	private void OnApplicationFocus(bool hasFocus)
	{
		SettingsStartGame.ApplicationFocus(hasFocus);
	}

	private void OnApplicationPause(bool pauseStatus)
	{
		SettingsStartGame.ApplicationFocus(!pauseStatus);
	}

	private void FixedUpdate()
	{
		currentTimerCrowd += Time.fixedDeltaTime;
		for (int i = 0; i < managerZombies.Length; i++)
		{
			if (wallRoom != 0f)
			{
				ManagerZombies obj = managerZombies[i];
				Vector3 position = base.transform.position;
				obj.periodScale = Mathf.Clamp01(1f - (position.x - startPosition.x) / wallRoom);
			}
			if (timeCrowd != 0f)
			{
				currentTimerCrowd = Mathf.Min(currentTimerCrowd, timeCrowd);
				managerZombies[i].periodScale = Mathf.Min(managerZombies[i].periodScale, (timeCrowd - currentTimerCrowd) / timeCrowd);
			}
		}
	}

	public void CheckAlive()
	{
		if (numZombies != null)
		{
			numZombies.text = ManagerZombies.aliveSceneTotal.ToString();
		}
		if (ManagerZombies.aliveSceneTotal == 0)
		{
			CancelInvoke("CheckAlive");
			currentNum = string.Empty;
			titleLevel.ShowCompelete(LoadScene);
		}
	}

	public void OnDeathPlayer()
	{
		CancelInvoke("CheckAlive");
		Invoke("LoadScene", 4f);
	}

	public void LoadScene()
	{
		if (Player.onStage.isDead)
		{
			if (saveZombiesNum)
			{
				leftTotal = new int[managerZombies.Length];
				for (int i = 0; i < managerZombies.Length; i++)
				{
					leftTotal[i] = managerZombies[i].totalAlive;
				}
			}
			SettingsStartGame.ResultGame(ManagerZombies.kills, 0f, Mathf.CeilToInt(Player.onStage.moneyHits));
			SettingsStartGame.GameOver(resetWeapon: true, playerDeath: true);
		}
		else
		{
			SettingsStartGame.NextCorridor();
			SettingsStartGame.ResultGame(ManagerZombies.kills, Player.onStage.scores, Mathf.CeilToInt(Player.onStage.moneyHits));
		}
		if (SettingsStartGame.numLives <= 0)
		{
			SceneManager.LoadScene(sceneGameOver);
		}
		else
		{
			SceneManager.LoadScene(sceneReplay);
		}
	}
}
